from rest_framework import serializers
from .models import Community, Owner, Maintenance, Announcement, SystemSetting
from django.contrib.auth.models import User
from django.utils import timezone

# 小区信息序列化器
class CommunitySerializer(serializers.ModelSerializer):
    class Meta:
        model = Community
        fields = '__all__'

# 业主信息序列化器
class OwnerSerializer(serializers.ModelSerializer):
    community_name = serializers.CharField(source='community.name', read_only=True)
    
    class Meta:
        model = Owner
        fields = ['id', 'community', 'community_name', 'name', 'gender', 'phone', 'id_card', 
                  'building', 'unit', 'room_number', 'house_area', 'move_in_date', 
                  'is_active', 'created_at', 'updated_at']

# 报修记录序列化器
class MaintenanceSerializer(serializers.ModelSerializer):
    owner_name = serializers.CharField(source='owner.name', read_only=True)
    owner_room = serializers.CharField(source='owner.room_number', read_only=True)
    handler_name = serializers.CharField(source='handler.username', read_only=True, allow_null=True)
    type_display = serializers.CharField(source='get_type_display', read_only=True)
    status_display = serializers.CharField(source='get_status_display', read_only=True)
    
    class Meta:
        model = Maintenance
        fields = ['id', 'owner', 'owner_name', 'owner_room', 'type', 'type_display', 'content', 
                  'status', 'status_display', 'image_url', 'report_time', 'processing_time', 
                  'completion_time', 'handler', 'handler_name', 'remarks', 'rating', 'feedback']
    
    # 处理状态变更的逻辑
    def update(self, instance, validated_data):
        status = validated_data.get('status')
        
        if status == 'processing' and instance.status != 'processing':
            instance.processing_time = timezone.now()
        elif status == 'completed' and instance.status != 'completed':
            instance.completion_time = timezone.now()
        
        return super().update(instance, validated_data)

# 公告管理序列化器
class AnnouncementSerializer(serializers.ModelSerializer):
    publisher_name = serializers.CharField(source='publisher.username', read_only=True)
    type_display = serializers.CharField(source='get_type_display', read_only=True)
    status_display = serializers.CharField(source='get_status_display', read_only=True)
    publisher = serializers.PrimaryKeyRelatedField(required=False, read_only=True)
    
    class Meta:
        model = Announcement
        fields = ['id', 'title', 'content', 'type', 'type_display', 'status', 'status_display', 
                  'publisher', 'publisher_name', 'publish_date', 'expire_date', 'is_sticky', 'views_count']

# 系统设置序列化器
class SystemSettingSerializer(serializers.ModelSerializer):
    updated_by_name = serializers.CharField(source='updated_by.username', read_only=True, allow_null=True)
    
    class Meta:
        model = SystemSetting
        fields = ['id', 'company_name', 'system_name', 'contact_phone', 'email', 
                  'address', 'logo_url', 'enable_notification', 'enable_self_repair', 
                  'enable_online_payment', 'updated_at', 'updated_by', 'updated_by_name']

# 用户信息序列化器（用于认证）
class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ['id', 'username', 'email', 'first_name', 'last_name', 'is_staff']

# 登录序列化器
class LoginSerializer(serializers.Serializer):
    username = serializers.CharField(required=True)
    password = serializers.CharField(required=True)